﻿Imports System.Diagnostics
Imports System.Runtime.InteropServices

Public Class Win32
    ' window positioning flags
    Shared ReadOnly HWND_TOPMOST As New IntPtr(-1)
    Shared ReadOnly HWND_NOTOPMOST As New IntPtr(-2)
    Shared ReadOnly HWND_TOP As New IntPtr(0)
    Shared ReadOnly HWND_BOTTOM As New IntPtr(1)

    Public Const SWP_NOMOVE As UInteger = &H2
    Public Const SWP_NOSIZE As UInteger = &H1
    Public Const SWP_SHOWWINDOW As UInteger = &H40

    ' http://www.developer.com/net/csharp/article.php/3347251
    ' and http://msdn.microsoft.com/en-us/library/ms633545(VS.85).aspx
    ' AND http://pinvoke.net/default.aspx/user32.SetWindowPos

    <DllImport("user32.dll", EntryPoint:="SetWindowPos")> _
    Public Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, _
     ByVal uFlags As UInteger) As Boolean
    End Function

    <DllImport("kernel32.dll")> _
Private Shared Function SetProcessWorkingSetSize(ByVal procHandle As IntPtr, ByVal min As Int32, ByVal max As Int32) As Boolean
    End Function

    <DllImport("user32.dll")> _
    Private Shared Function SetWindowLong(ByVal window As IntPtr, ByVal index As Integer, ByVal value As Integer) As Integer
    End Function

    <DllImport("user32.dll")> _
    Private Shared Function GetWindowLong(ByVal window As IntPtr, ByVal index As Integer) As Integer
    End Function

    ' alt tab code from http://bytes.com/forum/thread442047.html
    Private Const GWL_EXSTYLE As Integer = -20
    Private Const WS_EX_TOOLWINDOW As Integer = &H80

    Public Shared Sub HideFromAltTab(ByVal Handle As IntPtr)
        SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) Or WS_EX_TOOLWINDOW)
    End Sub

    Public Shared Sub SetProcessWorkingSetSize()
        SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1)
    End Sub

    Public Shared Sub SetWindowInBack(ByVal Handle As IntPtr)
        SetWindowPos(Handle, HWND_NOTOPMOST, 0, 0, 0, 0, _
         SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW)

        SetWindowPos(Handle, HWND_BOTTOM, 0, 0, 0, 0, _
         SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW)
    End Sub

    Public Shared Sub SetWindowInFront(ByVal Handle As IntPtr)
        SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0, _
         SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW)
    End Sub
End Class
